package com.gd.luxiaoyu.moduels.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gd.luxiaoyu.Constant;
import com.gd.luxiaoyu.moduels.entity.AdminTableEntity;
import com.gd.luxiaoyu.moduels.service.AdminTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@RequestMapping("right/admintable")
@RestController
public class AdminTableController {
    @Autowired
     private AdminTableService adminTableService;
    Constant constant =new Constant();
    /**
     * 添加新的管理员
     */
    @RequestMapping("/save")
    @ResponseBody
    public R<?> save(AdminTableEntity tableEntity){
        boolean  save =  adminTableService.save(tableEntity);

        return save? R.ok(constant.SUCCESSFUL):R.failed(constant.FAIL);
    }

    /*
    删除管理员
    * */
    @RequestMapping("/delete")
    @ResponseBody
    public R<?> Delete(AdminTableEntity adminTableEntity){
        boolean delete = adminTableService.removeById(adminTableEntity.getId());
        return  delete ? R.ok(constant.SUCCESSFUL)  : R.failed(constant.FAIL);
    }

    /*
    这个是查询所有的管理员
    * */
    @RequestMapping("query")
    @ResponseBody
    public Map<String,Object> QueryAll(Page<AdminTableEntity> adminTableEntityIPage, AdminTableEntity adminTableEntity){
        IPage<AdminTableEntity> adminMap = adminTableService.getAllAdmin(adminTableEntityIPage,adminTableEntity);
        Map<String,Object> map = new HashMap<>();
        map.put("Toals",adminMap.getTotal());
        map.put("Rows",adminMap.getRecords());
        return map;
    }

    /*
    * 更新管理员信息
    * */
    @RequestMapping("Update")
    @ResponseBody
    public R<?> Update(AdminTableEntity adminTableEntity){
        boolean update = adminTableService.updateById(adminTableEntity);
        return update ? R.ok(constant.SUCCESSFUL) : R.failed(constant.FAIL);
    }


    /*
     * 通过ID查询管理员
     * */
    @RequestMapping("queryById")
    @ResponseBody
    public AdminTableEntity queryById(AdminTableEntity adminTableEntity){
        AdminTableEntity update = adminTableService.getById(adminTableEntity.getId());
        return update;
    }


}
